﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FrameWork.Entity;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Collections;

namespace FrameWork.web.Code
{
    public class its_ServiceStatCs
    {
        public its_ServiceStatCs()
        {
            Microsoft.ApplicationBlocks.Data.SqlHelper.connectionString = ConfigurationManager.AppSettings["MsSql"].ToString();
        }
        /// <summary>
        /// 对部门进行统计
        /// </summary>
        /// <param name="bbb"></param>
        /// <returns></returns>
        public ArrayList its_StatByDeputy(int Statue, string BeginTime, string EndTime, int PageIndex, int PageSize)
        {
            SqlParameter[] sp = new SqlParameter[3]{
				new SqlParameter("@Statue",SqlDbType.Int),
				new SqlParameter("@BeginTime",SqlDbType.NVarChar,50),
				new SqlParameter("@EndTime",SqlDbType.NVarChar,50),
			};
            //
            if (Statue != -1)
            {
                sp[0].Value = Statue;
            }
            else
            {
                sp[0].Value = DBNull.Value;
            }

            sp[0].Direction = ParameterDirection.Input;


            //
            if (BeginTime != "")
            {
                sp[1].Value = BeginTime;
            }
            else
            {
                sp[1].Value = DBNull.Value;
            }

            sp[1].Direction = ParameterDirection.Input;


            //
            if (EndTime != "")
            {
                sp[2].Value = EndTime;
            }
            else
            {
                sp[2].Value = DBNull.Value;
            }

            sp[2].Direction = ParameterDirection.Input;


            try
            {
                DataSet dstResult = new DataSet();
                dstResult = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "dbo.its_StatByDeputy", sp);
                DataTable dtblResult = dstResult.Tables[0];
                ArrayList arr = new ArrayList();
                arr.Add(dtblResult.Rows.Count);
                int iNoREPCOUNT = 0;
                DataRow[] drNoREPCOUNTs = dstResult.Tables[0].Select("REPCOUNT=0");
                foreach (DataRow drNoREPCOUNT in drNoREPCOUNTs)
                {
                    drNoREPCOUNT["RATE"] = "0.00%";
                }
                DataRow[] drREPCOUNTs = dstResult.Tables[0].Select("REPCOUNT>0");
                int iRecordeCount = drREPCOUNTs.Length;
                decimal dRATE = 0.00m;
                for (int i = 0; i < iRecordeCount; i++)
                {
                    if (i < iRecordeCount - 1)
                    {
                        dRATE = dRATE + Math.Round(decimal.Parse(drREPCOUNTs[i]["REPCOUNT"].ToString()) / decimal.Parse(drREPCOUNTs[i]["AllCount"].ToString()) * 100, 2);
                        drREPCOUNTs[i]["RATE"] = Convert.ToString(Math.Round(decimal.Parse(drREPCOUNTs[i]["REPCOUNT"].ToString()) / decimal.Parse(drREPCOUNTs[i]["AllCount"].ToString()) * 100, 2)) + "%";
                    }
                    else
                    {
                        drREPCOUNTs[i]["RATE"] = Convert.ToString((100 - dRATE)) + "%";

                    }
                }
              
                DataTable dtblReturnResult = dtblResult.Clone();
                for (int i = 0; i < PageSize; i++)
                {
                    int iNowIndex = PageSize * (PageIndex - 1) + i;
                    if (iNowIndex > dtblResult.Rows.Count - 1)
                    {
                        break;
                    }
                    DataRow drReturnResult = dtblReturnResult.NewRow();
                    drReturnResult.ItemArray = dtblResult.Rows[iNowIndex].ItemArray;
                    dtblReturnResult.Rows.Add(drReturnResult);

                }
                arr.Add(dtblReturnResult);
                return arr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ArrayList its_StatByRePSN(string RepDeptNo, string Statue, string BeginTime, string EndTime, int PageIndex, int PageSize)
        {
            SqlParameter[] sp = new SqlParameter[4]{
				new SqlParameter("@RepDeptNo",SqlDbType.NVarChar,50),
				new SqlParameter("@Statue",SqlDbType.VarChar,10),
				new SqlParameter("@BeginTime",SqlDbType.NVarChar,50),
				new SqlParameter("@EndTime",SqlDbType.NVarChar,50),
			
			};
            //
            if (RepDeptNo != "-1")
            {
                sp[0].Value = RepDeptNo;
            }
            else
            {
                sp[0].Value = DBNull.Value;
            }

            sp[0].Direction = ParameterDirection.Input;


            //
            if (Statue != "-1")
            {
                sp[1].Value = Statue;
            }
            else
            {
                sp[1].Value = DBNull.Value;
            }

            sp[1].Direction = ParameterDirection.Input;


            //
            if (BeginTime != "")
            {
                sp[2].Value = BeginTime;
            }
            else
            {
                sp[2].Value = DBNull.Value;
            }

            sp[2].Direction = ParameterDirection.Input;


            //
            if (EndTime != "")
            {
                sp[3].Value = EndTime;
            }
            else
            {
                sp[3].Value = DBNull.Value;
            }

            sp[3].Direction = ParameterDirection.Input;


            try
            {
                DataSet dstResult = new DataSet();
                dstResult = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "dbo.its_StatByRePSN", sp);
                DataTable dtblResult = dstResult.Tables[0];
                DataTable dtblReturnResult = dtblResult.Clone();
                int iRecordCount = dtblResult.Rows.Count;
                ArrayList arr = new ArrayList();
                arr.Add(iRecordCount);
                for (int i = 0; i < PageSize; i++)
                {
                    int iNowIndex = PageSize * (PageIndex - 1) + i;
                    if (iNowIndex > dtblResult.Rows.Count - 1)
                    {
                        break;
                    }
                    DataRow drReturnResult = dtblReturnResult.NewRow();
                    drReturnResult.ItemArray = dtblResult.Rows[iNowIndex].ItemArray;
                    dtblReturnResult.Rows.Add(drReturnResult);
                }
                arr.Add(dtblReturnResult);

                return arr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 对维修人员进行统计
        /// </summary>
        /// <param name="bbb"></param>
        /// <returns></returns>
        public ArrayList its_StatBySolvPSN(string BeginTime, string EndTime, int PageIndex, int PageSize)
        {
            SqlParameter[] sp = new SqlParameter[2]{
				new SqlParameter("@BeginTime",SqlDbType.NVarChar,50),
				new SqlParameter("@EndTime",SqlDbType.NVarChar,50),
			
			};
            //
            if (BeginTime != "")
            {
                sp[0].Value = BeginTime;
            }
            else
            {
                sp[0].Value = DBNull.Value;
            }

            sp[0].Direction = ParameterDirection.Input;


            //
            if (EndTime != "")
            {
                sp[1].Value = EndTime;
            }
            else
            {
                sp[1].Value = DBNull.Value;
            }

            sp[1].Direction = ParameterDirection.Input;


            try
            {
                DataSet dstResult = new DataSet();
                dstResult = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "dbo.its_StatBySolvPSN", sp);
                DataTable dtblResult = dstResult.Tables[0];
                DataTable dtblReturnResult = dtblResult.Clone();
                int iRecordCount = dtblResult.Rows.Count;
                ArrayList arr = new ArrayList();
                arr.Add(iRecordCount);
                for (int i = 0; i < PageSize; i++)
                {
                    int iNowIndex = PageSize * (PageIndex - 1) + i;
                    if (iNowIndex > dtblResult.Rows.Count - 1)
                    {
                        break;
                    }
                    DataRow drReturnResult = dtblReturnResult.NewRow();
                    drReturnResult.ItemArray = dtblResult.Rows[iNowIndex].ItemArray;
                    dtblReturnResult.Rows.Add(drReturnResult);
                }
                arr.Add(dtblReturnResult);

                return arr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 对故障分类进行统计
        /// </summary>
        /// <param name="bbb"></param>
        /// <returns></returns>
        public ArrayList its_StatByTroub(string Statue, string BeginTime, string EndTime, int PageIndex, int PageSize)
        {
            SqlParameter[] sp = new SqlParameter[3]{
                new SqlParameter("@Statue",SqlDbType.NVarChar,50),
				new SqlParameter("@BeginTime",SqlDbType.NVarChar,50),
				new SqlParameter("@EndTime",SqlDbType.NVarChar,50),
			
			};
            if (Statue != "-1")
            {
                sp[0].Value = Statue;
            }
            else
            {
                sp[0].Value = DBNull.Value;
            }
            //
            if (BeginTime != "")
            {
                sp[1].Value = BeginTime;
            }
            else
            {
                sp[1].Value = DBNull.Value;
            }

            sp[1].Direction = ParameterDirection.Input;


            //
            if (EndTime != "")
            {
                sp[2].Value = EndTime;
            }
            else
            {
                sp[2].Value = DBNull.Value;
            }

            sp[2].Direction = ParameterDirection.Input;


            try
            {
                DataSet dstResult = new DataSet();
                dstResult = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "dbo.its_StatByTroub", sp);
                DataTable dtblResult = dstResult.Tables[0];
                DataTable dtblReturnResult = dtblResult.Clone();
                int iRecordCount = dtblResult.Rows.Count;
                ArrayList arr = new ArrayList();
                arr.Add(iRecordCount);

                int iNoREPCOUNT = 0;
                DataRow[] drNoREPCOUNTs = dstResult.Tables[0].Select("DocCOUNT=0");
                foreach (DataRow drNoREPCOUNT in drNoREPCOUNTs)
                {
                    drNoREPCOUNT["RATE"] = "0.00%";
                }
                DataRow[] drREPCOUNTs = dstResult.Tables[0].Select("DocCOUNT>0");
                int iRecordeCount = drREPCOUNTs.Length;
                decimal dRATE = 0.00m;
                for (int i = 0; i < iRecordeCount; i++)
                {
                    if (i < iRecordeCount - 1)
                    {
                        dRATE = dRATE + Math.Round(decimal.Parse(drREPCOUNTs[i]["DocCOUNT"].ToString()) / decimal.Parse(drREPCOUNTs[i]["AllCount"].ToString()) * 100, 2);
                        drREPCOUNTs[i]["RATE"] = Convert.ToString(Math.Round(decimal.Parse(drREPCOUNTs[i]["DocCOUNT"].ToString()) / decimal.Parse(drREPCOUNTs[i]["AllCount"].ToString()) * 100, 2)) + "%";
                    }
                    else
                    {
                        drREPCOUNTs[i]["RATE"] = Convert.ToString((100 - dRATE)) + "%";

                    }
                }


                for (int i = 0; i < PageSize; i++)
                {
                    int iNowIndex = PageSize * (PageIndex - 1) + i;
                    if (iNowIndex > dtblResult.Rows.Count - 1)
                    {
                        break;
                    }
                    DataRow drReturnResult = dtblReturnResult.NewRow();
                    drReturnResult.ItemArray = dtblResult.Rows[iNowIndex].ItemArray;
                    dtblReturnResult.Rows.Add(drReturnResult);
                }
                arr.Add(dtblReturnResult);

                return arr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 对问题分类进行统计
        /// </summary>
        /// <param name="bbb"></param>
        /// <returns></returns>
        public ArrayList its_StatByProbType(string Statue, string BeginTime, string EndTime, int PageIndex, int PageSize)
        {
            SqlParameter[] sp = new SqlParameter[3]{
                new SqlParameter("@Statue",SqlDbType.NVarChar,50),
				new SqlParameter("@BeginTime",SqlDbType.NVarChar,50),
				new SqlParameter("@EndTime",SqlDbType.NVarChar,50),
			
			};
            if (Statue != "-1")
            {
                sp[0].Value = Statue;
            }
            else
            {
                sp[0].Value = DBNull.Value;
            }
            //
            if (BeginTime != "")
            {
                sp[1].Value = BeginTime;
            }
            else
            {
                sp[1].Value = DBNull.Value;
            }

            sp[1].Direction = ParameterDirection.Input;


            //
            if (EndTime != "")
            {
                sp[2].Value = EndTime;
            }
            else
            {
                sp[2].Value = DBNull.Value;
            }

            sp[2].Direction = ParameterDirection.Input;


            try
            {
                DataSet dstResult = new DataSet();
                dstResult = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "dbo.its_StatByProbType", sp);
                DataTable dtblResult = dstResult.Tables[0];
                DataTable dtblReturnResult = dtblResult.Clone();
                int iRecordCount = dtblResult.Rows.Count;
                ArrayList arr = new ArrayList();
                arr.Add(iRecordCount);

                int iNoREPCOUNT = 0;
                DataRow[] drNoREPCOUNTs = dstResult.Tables[0].Select("DocCOUNT=0");
                foreach (DataRow drNoREPCOUNT in drNoREPCOUNTs)
                {
                    drNoREPCOUNT["RATE"] = "0.00%";
                }
                DataRow[] drREPCOUNTs = dstResult.Tables[0].Select("DocCOUNT>0");
                int iRecordeCount = drREPCOUNTs.Length;
                decimal dRATE = 0.00m;
                for (int i = 0; i < iRecordeCount; i++)
                {
                    if (i < iRecordeCount - 1)
                    {
                        dRATE = dRATE + Math.Round(decimal.Parse(drREPCOUNTs[i]["DocCOUNT"].ToString()) / decimal.Parse(drREPCOUNTs[i]["AllCount"].ToString()) * 100, 2);
                        drREPCOUNTs[i]["RATE"] = Convert.ToString(Math.Round(decimal.Parse(drREPCOUNTs[i]["DocCOUNT"].ToString()) / decimal.Parse(drREPCOUNTs[i]["AllCount"].ToString()) * 100, 2)) + "%";
                    }
                    else
                    {
                        drREPCOUNTs[i]["RATE"] = Convert.ToString((100 - dRATE)) + "%";

                    }
                }


                for (int i = 0; i < PageSize; i++)
                {
                    int iNowIndex = PageSize * (PageIndex - 1) + i;
                    if (iNowIndex > dtblResult.Rows.Count - 1)
                    {
                        break;
                    }
                    DataRow drReturnResult = dtblReturnResult.NewRow();
                    drReturnResult.ItemArray = dtblResult.Rows[iNowIndex].ItemArray;
                    dtblReturnResult.Rows.Add(drReturnResult);
                }
                arr.Add(dtblReturnResult);

                return arr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
